package com.qlzb.taskmanagement.sys;


import com.qlzb.taskmanagement.entity.CronInfoEntity;
import com.qlzb.taskmanagement.service.cron.CronInfoService;
import com.qlzb.taskmanagement.task.CreateCrawlerTask;
import com.qlzb.taskmanagement.vo.quartzVo.AreaUserList;
import com.qlzb.taskmanagement.vo.quartzVo.ParamProperties;
import com.qlzb.taskmanagement.vo.quartzVo.QuartzUserList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class StartAddDataListener implements ApplicationListener<ContextRefreshedEvent> {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    CreateCrawlerTask startTask;

    @Autowired
    private CronInfoService cronInfoService;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event)
    {
        if(event.getApplicationContext().getParent() == null)//root application context 没有parent，他就是老大.
        {

            //加载数据到内存
            List<CronInfoEntity> cronInfoEntities = cronInfoService.findAll();
            if (cronInfoEntities.size() > 0){
                QuartzUserList.cronEntitys = cronInfoEntities;
            }
            //动态创建任务列表 启动定时任务
            startTask.startMainCrawlerTask();
        }
    }
}
